package class07;

/**
 * 给定一个整型数组arr代表数值不同的纸牌排成一条线
 * 玩家A和玩家B依次拿走每一张牌规定玩家A先拿玩家B后拿
 * 但每个玩家每次只能拿最左或者最右的牌
 * 玩家A和玩家B智商一样请返回最后获胜者的分数
 */
public class Code02_CardsInLine {
    static int win1(int[] arr) {
        int first = f1(0, arr.length - 1, arr);
        int second = g1(0, arr.length - 1, arr);
        return Math.max(first, second);
    }

    static int f1(int l, int r, int[] arr) {
        if (l == r) {
            return arr[l];
        }

        int p1 = arr[l] + g1(l + 1, r, arr);
        int p2 = arr[r] + g1(l, r - 1, arr);
        return Math.max(p1, p2);
    }

    static int g1(int l, int r, int[] arr) {
        if (l == r) {
            return 0;
        } else {

            int p1 = f1(l + 1, r, arr);
            int p2 = f1(l, r - 1, arr);
            return Math.min(p1, p2);
        }

    }

    static int win2(int[] arr) {
        int N = arr.length;

        int[][] fmap = new int[N][N];
        int[][] gmap = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                fmap[i][j] = -1;
                gmap[i][j] = -1;
            }
        }

        int first = f2(0, arr.length - 1, arr, fmap, gmap);
        int second = g2(0, arr.length - 1, arr, fmap, gmap);
        return Math.max(first, second);
    }

    static int f2(int l, int r, int[] arr, int[][] fmap, int[][] gmap) {

        if (fmap[l][r] != -1) {
            return fmap[l][r];
        }
        int ans = 0;
        if (l == r) {
            ans = arr[l];
        } else {

            int p1 = arr[l] + g2(l + 1, r, arr, fmap, gmap);
            int p2 = arr[r] + g2(l, r - 1, arr, fmap, gmap);

            ans = Math.max(p1, p2);
        }
        fmap[l][r] = ans;
        return ans;
    }

    static int g2(int l, int r, int[] arr, int[][] fmap, int[][] gmap) {
        if (gmap[l][r] != -1) {
            return gmap[l][r];
        }

        int ans = 0;
        if (l != r) {
            int p1 = f2(l + 1, r, arr, fmap, gmap);
            int p2 = f2(l, r - 1, arr, fmap, gmap);
            ans = Math.min(p1, p2);
        }
        gmap[l][r] = ans;
        return ans;
    }

    public static int win3(int[] arr) {
        if (arr == null || arr.length == 0) {
            return 0;
        }
        int N = arr.length;
        int[][] fmap = new int[N][N];
        int[][] gmap = new int[N][N];
        for (int i = 0; i < N; i++) {
            fmap[i][i] = arr[i];
        }
        for (int startCol = 1; startCol < N; startCol++) {
            int L = 0;
            int R = startCol;
            while (R < N) {
                fmap[L][R] = Math.max(arr[L] + gmap[L + 1][R], arr[R] + gmap[L][R - 1]);
                gmap[L][R] = Math.min(fmap[L + 1][R], fmap[L][R - 1]);
                L++;
                R++;
            }
        }
        return Math.max(fmap[0][N - 1], gmap[0][N - 1]);
    }

    public static void main(String[] args) {
        int[] arr = {5, 7, 4, 5, 8, 1, 6, 0, 3, 4, 6, 1, 7};
        System.out.println(win1(arr));
        System.out.println(win2(arr));
        System.out.println(win3(arr));
    }
}
